package com.data4.supplier.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data4.feignapi.dto.CategoryDTO;
import com.data4.feignapi.dto.CategoryUpdateDTO;
import com.data4.feignapi.dto.PageDTO;
import com.data4.feignapi.dto.Result;
import com.data4.feignapi.entity.Category;
import com.data4.feignapi.entity.Standards;
import com.data4.feignapi.vo.CategoryAndStandardsVO;
import com.data4.supplier.mapper.CategoryMapper;
import com.data4.supplier.service.CategoryService;
import com.data4.supplier.service.StandardsService;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private StandardsService standardsService;

    @Override
    public Result addCategory(CategoryDTO categoryAndStandard, Integer supplierId) {
        Standards standards = BeanUtil.toBean(categoryAndStandard, Standards.class);
        standardsService.save(standards);
        Integer standardId = standardsService.lambdaQuery(standards)
                .one().getStandardId();
        Category category = BeanUtil.toBean(categoryAndStandard, Category.class);
        category.setStandardId(standardId);
        category.setSupplierId(supplierId);
        save(category);
        return Result.ok();
    }

    @Override
    public Result updateCategory(CategoryUpdateDTO category,
                                 Integer categoryId, Integer supplierId) {
        Category category1 = BeanUtil.toBean(category, Category.class);
        boolean update = update(category1,
                new LambdaQueryWrapper<Category>()
                        .eq(Category::getCategoryId, categoryId)
                        .eq(Category::getSupplierId, supplierId)
        );
        standardsService.updateStandards(category.getStandardsUpdateDTO(),category.getStandardId());
        return update ? Result.ok() : Result.fail("更新失败");
    }

    @Override
    public Result deleteCategory(List<Integer> categoryIdList, Integer supplierId) {
        List<Integer> standardsIds = categoryIdList
                .stream()
                .map(id -> {
                    Integer standardId = lambdaQuery()
                            .eq(Category::getCategoryId, id)
                            .one()
                            .getStandardId();
                    return standardId;
                })
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(standardsIds)) return Result.fail("没有对应规格");
        remove(new LambdaQueryWrapper<Category>()
                .in(Category::getCategoryId, categoryIdList)
                .eq(Category::getSupplierId, supplierId));
        standardsService.remove(new LambdaQueryWrapper<Standards>()
                .in(Standards::getStandardId,standardsIds));
        return Result.ok();
    }

    @Override
    public Result getCategory(Integer current, Integer num, Integer supplierId) {
        Page<Category> categoryPage = lambdaQuery()
                .eq(Category::getSupplierId, supplierId)
                .page(new Page<>(current, num));
        List<CategoryAndStandardsVO> categoryAndStandardsVOS = categoryPage
                .getRecords()
                .stream()
                .map(this::getCategoryAndStandardsVO)
                .collect(Collectors.toList());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setList(categoryAndStandardsVOS);
        pageDTO.setTotalNum(categoryPage.getTotal());
        return Result.ok(pageDTO);
    }

    @Override
    public Result getCategoryByNameLike(String parentName, String childName,
                                        Integer current, Integer num, Integer supplierId) {
        Page<Category> categoryPage = lambdaQuery()
                .eq(Category::getSupplierId, supplierId)
                .like(Category::getCategoryParentName, parentName)
                .like(childName!=null,
                        Category::getCategoryChildName, childName)
                .page(new Page<>(current, num));
        List<Category> categoryList = categoryPage
                .getRecords();
        List<CategoryAndStandardsVO> categoryAndStandardsVOS = categoryList.stream()
                .map(this::getCategoryAndStandardsVO)
                .collect(Collectors.toList());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setList(categoryAndStandardsVOS);
        pageDTO.setTotalNum(categoryPage.getTotal());
        return Result.ok(pageDTO);
    }

    @Override
    public CategoryAndStandardsVO getCategoryAndStandardsVO(Integer categoryId,
                                                            Integer supplierId) {
        Category category = lambdaQuery()
                .eq(supplierId!=null,Category::getSupplierId, supplierId)
                .eq(Category::getCategoryId, categoryId)
                .one();
        if (BeanUtil.isEmpty(category)) return null;
        CategoryAndStandardsVO categoryAndStandardsVO = getCategoryAndStandardsVO(category);
        return categoryAndStandardsVO;
    }

    private @NotNull CategoryAndStandardsVO getCategoryAndStandardsVO(@NotNull Category category) {
        Standards standards = standardsService.lambdaQuery()
                .eq(Standards::getStandardId, category.getStandardId())
                .one();
        CategoryAndStandardsVO categoryAndStandardsVO = BeanUtil
                .toBean(category, CategoryAndStandardsVO.class);
        categoryAndStandardsVO.setStandards(standards);
        return categoryAndStandardsVO;
    }
}
